﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Interface;
using App.Model;
using App.Model.MoBanModel;
using App.MVC.App_Start;
using Newtonsoft.Json;

namespace App.MVC.Controllers
{
    public class MoBanConfigController : BaseController
    {
        public IJiaoZhiGong _IJiaoZhiGong { set; get; }
        public IMoBan _IMoBan { set; get; }
        // GET: MoBanConfig
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 保存工作計劃
        /// </summary>
        /// <param name="model"></param>
        public void SaveGongZuoJiHua(GongZuoJiHua model)
        {
            model.cjsj = DateTime.Now;
            model.cjrname = SiteInfo.GetUserName();
            //工作计划 = 1,
            //绩效考核 = 2
            model.mbtype = 1;
            string str = JsonConvert.SerializeObject(model);
            _IMoBan.AddMoBan(str);
        }

        public void SaveJiXiao(JiXiao model)
        {
            model.cjsj = DateTime.Now;
            model.cjrname = SiteInfo.GetUserName();
            model.mbtype = 2;
            string str = JsonConvert.SerializeObject(model);
            _IMoBan.AddJiXiaoMoBan(str);
        }
        public JsonResult GetGZJHs()
        {
            var str = _IMoBan.GetGZJHList();
            List<GongZuoJiHua> tt = JsonConvert.DeserializeObject<List<GongZuoJiHua>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetGZJH(int mbid)
        {
            var str = _IMoBan.GetGZJHInfo(mbid);
            GongZuoJiHua tt = JsonConvert.DeserializeObject<GongZuoJiHua>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetJXs()
        {
            var str = _IMoBan.GetJXList();
            List<JiXiao> tt = JsonConvert.DeserializeObject<List<JiXiao>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public void DelGZJH(int mbid)
        {
            _IMoBan.DelGZJH(mbid);
        }
        public void DelJX(int mbid)
        {
            _IMoBan.DelJX(mbid);
        }

        public ActionResult ShengChengKaoHeBiao()
        {
            return View();
        }

        public void CreateKaoHeBiao(int ksid)
        {
            string datetimes = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();
            _IMoBan.CreateKaoHeBiao(ksid, SiteInfo.GetUserName(), datetimes);
        }

        public void ReviewKaoHeBiao(int ksid)
        {
            _IMoBan.ReviewKaoHeBiao(ksid, SiteInfo.GetUserName());
        }

        #region 新模板
        public ActionResult MoBanPage()
        {
            return View();
        }

        public JsonResult GetMoBanList()
        {
            var str = _IMoBan.GetMoBanList();
            List<MoBan_Main> tt = JsonConvert.DeserializeObject<List<MoBan_Main>>(str);
            foreach (var item in tt)
            {
                item.MoBan_Paths = item.MoBan_Paths.OrderBy(s => s.shunxu).ToList();
                foreach (var item2 in item.MoBan_Paths)
                {
                    item2.MoBan_Contents = item2.MoBan_Contents.OrderBy(s => s.shunxu).ToList();
                    foreach (var item3 in item2.MoBan_Contents)
                    {
                        item3.MoBan_Answers = item3.MoBan_Answers.OrderBy(s => s.shunxu).ToList();
                    }
                }
            }
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetControlList()
        {
            var str = _IMoBan.GetControlList();
            List<MoBan_Control> tt = JsonConvert.DeserializeObject<List<MoBan_Control>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public JsonResult GetSearchMoBanList(string keyword)
        {
            var str = _IMoBan.GetSearchMoBanList(keyword);
            if (string.IsNullOrEmpty(str))
            {
                return Json(new List<MoBan_Main>(), JsonRequestBehavior.AllowGet);
            }
            List<MoBan_Main> tt = JsonConvert.DeserializeObject<List<MoBan_Main>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public JsonResult GetMoBanType()
        {
            var str = _IMoBan.GetMoBanType();
            if (string.IsNullOrEmpty(str))
            {
                return Json(new Dictionary<string, int>(), JsonRequestBehavior.AllowGet);
            }
            Dictionary<string, int> tt = JsonConvert.DeserializeObject<Dictionary<string, int>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public void SaveMoBan_Main(MoBan_Main mb)
        {
            _IMoBan.SaveMoBan_Main(mb, SiteInfo.UserId);
        }

        [HttpGet]
        public void DelMoBan(int mbid)
        {
            _IMoBan.DelMoBan(mbid);
        }
        [HttpGet]
        public JsonResult GetExceptMySelfMoBan(int mbid)
        {
            var str = _IMoBan.GetExceptMySelfMoBan(mbid, "");
            if (string.IsNullOrEmpty(str))
            {
                return Json(new List<MoBan_Main>(), JsonRequestBehavior.AllowGet);
            }
            List<MoBan_Main> tt = JsonConvert.DeserializeObject<List<MoBan_Main>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public int PathUpdate(MoBan_Path model, int mbid)
        {
            if (model.ljid > 0)
            {
                _IMoBan.PathUpdate(model, mbid);
                return model.ljid;
            }
            else
            {
                return _IMoBan.PathAdd(model, mbid);
            }

        }

        public int ContentUpdate(MoBan_Content model, int mbid, int ljid)
        {
            if (model.nrid > 0)
            {
                _IMoBan.ContentUpdate(model, mbid, ljid);
                return model.nrid;
            }
            else
            {
                return _IMoBan.ContentAdd(model, mbid, ljid);
            }
        }

        public int AnswerUpdate(MoBan_Answer model, int mbid, int ljid, int nrid)
        {
            if (model.aid > 0)
            {
                _IMoBan.AnswerUpdate(model, mbid, ljid, nrid);
                return model.aid;
            }
            else
            {
                return _IMoBan.AnswerAdd(model, mbid, ljid, nrid);
            }
        }
        [HttpGet]
        public void PathDel(int mbid, int ljid)
        {
            _IMoBan.PathDel(mbid, ljid);
        }
        [HttpGet]
        public void ContentDel(int mbid, int ljid, int nrid)
        {
            _IMoBan.ContentDel(mbid, ljid, nrid);
        }
        [HttpGet]
        public void AnswerDel(int mbid, int ljid, int nrid, int answerid)
        {
            _IMoBan.AnswerDel(mbid, ljid, nrid, answerid);
        }
        #endregion

        #region 考核范围设置

        public ActionResult KaoHeFanWeiSetting()
        {
            return View();
        }

        public JsonResult GetJiaoZhiGongs()
        {
            string str = _IJiaoZhiGong.GetEmployees("", 0, 0, 1, 1000);
            JiaoZhiGongList tt = new JiaoZhiGongList();

            if (!string.IsNullOrWhiteSpace(str))
            {
                tt = JsonConvert.DeserializeObject<JiaoZhiGongList>(str);
            }
            return Json(tt.data, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetKaoHeFanWeis()
        {
            string str = _IMoBan.GetKaoHeFanWeis("");
            List<KaoHeFanWei> tt = JsonConvert.DeserializeObject<List<KaoHeFanWei>>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public JsonResult CreateKaoHeFanWei(string userinfoid)
        {
            int fwid = _IMoBan.AddKaoHeFanWei(userinfoid);
            dynamic tt = new { khfwid = fwid };
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public void DelKaoHeFanWei(int khfwid)
        {
            _IMoBan.DelKaoHeFanWei(khfwid);
        }

        public JsonResult GetKaoHeFanWeiInfo(int khfwid)
        {
            string str = _IMoBan.GetKaoHeFanWei(khfwid);
            KaoHeFanWei tt = JsonConvert.DeserializeObject<KaoHeFanWei>(str);
            return Json(tt, JsonRequestBehavior.AllowGet);
        }

        public void SelectKaoHeXiangMu(int khfwryid, int ljid)
        {
            _IMoBan.AddKaoHePath(khfwryid, ljid);
        }

        public void DelKaoHeXiangMu(int khfwryid, int nrid)
        {
            _IMoBan.DelKaoHePath(khfwryid, nrid);
        }

        public void SelectBeiKaoHeRenYuan(int khfwryid, string userid)
        {
            _IMoBan.AddBeiKaoHeJiaoZhiGong(khfwryid, userid);
        }

        public void DelBeiKaoHeRenYuan(int khfwryid, string userid)
        {
            _IMoBan.DelBeiKaoHeJiaoZhiGong(khfwryid, userid);
        }
        #endregion
    }
}